package com.un.ebs.sys.controller;


import com.un.ebs.core.enums.PageTypeEnum;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.dto.PageAddDto;
import com.un.ebs.sys.dto.PageUpdateDto;
import com.un.ebs.sys.service.PageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-04-19
 */
@RestController
@RequestMapping("/page")
@Api(value = "sys-page", tags = "")
public class PageController {

    @Autowired
    private PageService service;

    @PostMapping("add")
    @ApiOperation(value = "添加")
    public WebResult save(@RequestBody PageAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public WebResult update(@RequestBody PageUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("copyTo")
    @ApiOperation(value = "复制")
    public WebResult copyTo(@RequestParam String sourceId, @RequestParam String newTitle) {
        service.copyTo(sourceId, newTitle);
        return WebUtils.success();
    }

    @PostMapping("moveTo")
    @ApiOperation(value = "移动")
    public WebResult moveTo(@RequestParam String id, @RequestParam String classId) {
        service.moveTo(id, classId);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取详情")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询")
    public WebResult list(@RequestParam(required = false) String title, @RequestParam(required = false) String classId, @RequestParam(required = false) PageTypeEnum type, @RequestParam int pageIndex, @RequestParam int pageSize) {
        return WebUtils.success(service.query(title, classId, type, pageIndex, pageSize));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public WebResult deleteById(@PathVariable String id) {
        service.delete(id);
        return WebUtils.success();
    }

}